<?php
declare (strict_types = 1);

namespace app\common\extend;

use think\facade\Cache;

class RedisExtend
{

    protected $redis;

    public function __construct()
    {
        $this->redis = Cache::store('redis')->handler();
    }

    /**
     * 设置缓存（哈希）
     */
    public function hMSet($name = '', $value = [], $config = [])
    {

        $set = $this->redis->hMSet($name, $value);

        if ($set !== true) exception();

        if (isset($config['expire'])) $this->expire($name, $config['expire']);

        return true;
    }

    /**
     * 设置过期时间
     */
    protected function expire($name = '', $expire = 0)
    {

        $expire = $this->redis->expire($name, $expire);

        if ($expire !== true) exception();

        return true;
    }

    /**
     *  删除
     */
    public function del($name = '')
    {
        return $this->redis->del($name);
    }

    /**
     * 读取缓存（哈希、所有）
     */
    public function hGetAll($name = '')
    {
        return $this->redis->hGetAll($name);
    }

}
